package homework_5.write;

public class Player {
    private int hp = 100;

    public int getHP() {
        return hp;
    }

    public void setHP(int hp) {
        this.hp = hp;
    }


    synchronized void beAttacked(int y){
        for (int i = 0; i < 3; i++) {
            System.out.println(Thread.currentThread().getName() + " attack...");
            this.setHP(getHP() - y);
            System.out.println(Thread.currentThread().getName() + ": 当前player的hp值= " + getHP());
            if (getHP() <= 0) {
                System.out.println(Thread.currentThread().getName() + ": player is dead.");
                break;
            }
            try {
                Thread.sleep(100);
            }
            catch (InterruptedException e) {
            }
        }
        System.out.println(Thread.currentThread().getName() + " end.");
    }

}
